package handler

import (
	"2301a/yuekao/api/request"
	"2301a/yuekao/consts"
	"2301a/yuekao/internal/dao"
	"2301a/yuekao/internal/model"
	"2301a/yuekao/internal/queue"
	"2301a/yuekao/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
)

func HouseAdd(c *gin.Context) {
	var req request.HouseAddReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "参数信息错误",
			"data":    err.Error(),
		})
		return
	}
	if GetRestConsort(req.Title, c.GetUint("userId"), "房源发布") {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "敏感词，添加失败",
			"data":    nil,
		})
		return
	}

	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.Id == 2 || user.Id == 3) {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "该权限不足",
			"data":    nil,
		})
		return
	}
	house := model.House{
		UserId:        c.GetUint("userId"),
		Title:         req.Title,
		Price:         req.Price,
		Area:          req.Area,
		City:          req.City,
		Address:       req.Address,
		CommunityName: req.CommunityName,
		PictureUrl:    req.PictureUrl,
		VideoUrl:      req.VideoUrl,
	}
	dao.Create(&house)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "房源发布成功",
		"data":    true,
	})

}

func GetRestConsort(message string, userId uint, dName string) bool {

	if !pkg.GetRestConsort(message, userId, dName) {
		return true
	}

	dao.Create(&model.Safe{
		UserId:  userId,
		Content: message,
		BName:   dName,
	})
	return false

}

func GetHouseList(c *gin.Context) {
	var req request.GetHouseListReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "参数信息错误",
			"data":    err.Error(),
		})
		return
	}
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "",
	}
	var list []model.House
	paginate, _ := dao.Paginate(&model.House{Title: req.Title}, pageDto, &list)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "房源列表成功",
		"data":    paginate,
	})
}

func GetHouseDetails(c *gin.Context) {
	var req request.GetHouseDetailsReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "参数信息错误",
			"data":    err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.Id == 2 || user.Id == 3) {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "该权限不足",
			"data":    nil,
		})
		return
	}
	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "房源不存在",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "房源详情成功",
		"data":    house,
	})

}

func UpdateHouse(c *gin.Context) {
	var req request.UpdateHouseReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "参数信息错误",
			"data":    err.Error(),
		})
		return
	}

	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.Id == 1) {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "该权限不足",
			"data":    nil,
		})
		return
	}
	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "房源不存在",
			"data":    nil,
		})
		return
	}
	if req.Price > 0 && (req.Price != house.Price) {
		queue.PublishPub(consts.HOUSE_PRICE_UPDATE, &model.House{
			Id:     req.Id,
			UserId: c.GetUint("userId"),
			Price:  req.Price,
		})
	}
	dao.Update(&model.House{
		Id:            req.Area,
		UserId:        c.GetUint("userId"),
		Title:         req.Title,
		Price:         req.Price,
		Area:          req.Area,
		City:          req.City,
		Address:       req.Address,
		CommunityName: req.CommunityName,
		PictureUrl:    req.PictureUrl,
		VideoUrl:      req.VideoUrl,
		Status:        req.Status,
	})
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "房源价格修改成功",
		"data":    true,
	})
}

func HouseFile(c *gin.Context) {
	file, _ := c.FormFile("file")
	ext := path.Ext(file.Filename)
	if ext != ".png" && ext != ".jpg" && ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "上传文件格式只能是.png和.jpg或者.mp4",
			"data":    nil,
		})
		return
	}
	if file.Size > 1024*1024*200 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "上传文件大小不能超过200MB",
			"data":    nil,
		})
		return
	}
	dst := "D:\\phpstudy_pro\\WWW\\" + file.Filename

	c.SaveUploadedFile(file, dst)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "上传文件成功",
		"data":    fmt.Sprintf("http://localhost/%s", file.Filename),
	})
}
